﻿using System;
using System . Collections . Generic;
using System . IO;
using System . Linq;
using System . Runtime . InteropServices . WindowsRuntime;
using Windows . Foundation;
using Windows . Foundation . Collections;
using Windows . UI . Xaml;
using Windows . UI . Xaml . Controls;
using Windows . UI . Xaml . Controls . Primitives;
using Windows . UI . Xaml . Data;
using Windows . UI . Xaml . Input;
using Windows . UI . Xaml . Media;
using Windows . UI . Xaml . Navigation;

namespace WenceyWang . RichMan4L . Maps . MapDrawers . UWPXaml2DMapDrawer . BlockResources
{
	public sealed partial class NormalRoadBlock : UserControl, IMapObjectView<NormalRoad>
	{
		public NormalRoadBlock ( )
		{
			this . InitializeComponent ( );
			this . Loaded += NormalRoadBlock_Loaded;
		}

		private void NormalRoadBlock_Loaded ( object sender , RoutedEventArgs e )
		{
			Update ( );
		}

		void UpdateDirection ( )
		{
			switch ( SourceObject . GetAzimuth ( SourceObject . ForwardRoad ) )
			{
				case BlockAzimuth . None:
					{
						VisualStateManager . GoToState ( this , nameof ( None ) , false );
						break;
					}
				case BlockAzimuth . Up:
					{
						switch ( SourceObject . GetAzimuth ( SourceObject . BackwardRoad ) )
						{
							case BlockAzimuth . None:
								{
									VisualStateManager . GoToState ( this , nameof ( None ) , false );
									break;
								}
							case BlockAzimuth . Up:
								{
									VisualStateManager . GoToState ( this , nameof ( None ) , false );
									break;
								}
							case BlockAzimuth . Down:
								{
									VisualStateManager . GoToState ( this , nameof ( UpDown ) , false );
									break;
								}
							case BlockAzimuth . Left:
								{
									VisualStateManager . GoToState ( this , nameof ( UpLeft ) , false );
									break;
								}
							case BlockAzimuth . Right:
								{
									VisualStateManager . GoToState ( this , nameof ( UpRight ) , false );
									break;
								}
						}
						break;
					}
				case BlockAzimuth . Down:
					{
						switch ( SourceObject . GetAzimuth ( SourceObject . BackwardRoad ) )
						{
							case BlockAzimuth . None:
								{
									VisualStateManager . GoToState ( this , nameof ( None ) , false );
									break;
								}
							case BlockAzimuth . Up:
								{
									VisualStateManager . GoToState ( this , nameof ( UpDown ) , false );
									break;
								}
							case BlockAzimuth . Down:
								{
									VisualStateManager . GoToState ( this , nameof ( None ) , false );
									break;
								}
							case BlockAzimuth . Left:
								{
									VisualStateManager . GoToState ( this , nameof ( DownLeft ) , false );
									break;
								}
							case BlockAzimuth . Right:
								{
									VisualStateManager . GoToState ( this , nameof ( DownRight ) , false );
									break;
								}
						}
						break;
					}
				case BlockAzimuth . Left:
					{
						switch ( SourceObject . GetAzimuth ( SourceObject . BackwardRoad ) )
						{
							case BlockAzimuth . None:
								{
									VisualStateManager . GoToState ( this , nameof ( None ) , false );
									break;
								}
							case BlockAzimuth . Up:
								{
									VisualStateManager . GoToState ( this , nameof ( UpLeft ) , false );
									break;
								}
							case BlockAzimuth . Down:
								{
									VisualStateManager . GoToState ( this , nameof ( DownLeft ) , false );
									break;
								}
							case BlockAzimuth . Left:
								{
									VisualStateManager . GoToState ( this , nameof ( None ) , false );
									break;
								}
							case BlockAzimuth . Right:
								{
									VisualStateManager . GoToState ( this , nameof ( LeftRight ) , false );
									break;
								}
						}
						break;
					}
				case BlockAzimuth . Right:
					{
						switch ( SourceObject . GetAzimuth ( SourceObject . BackwardRoad ) )
						{
							case BlockAzimuth . None:
								{
									VisualStateManager . GoToState ( this , nameof ( None ) , false );
									break;
								}
							case BlockAzimuth . Up:
								{
									VisualStateManager . GoToState ( this , nameof ( UpRight ) , false );
									break;
								}
							case BlockAzimuth . Down:
								{
									VisualStateManager . GoToState ( this , nameof ( DownRight ) , false );
									break;
								}
							case BlockAzimuth . Left:
								{
									VisualStateManager . GoToState ( this , nameof ( LeftRight ) , false );
									break;
								}
							case BlockAzimuth . Right:
								{
									VisualStateManager . GoToState ( this , nameof ( None ) , false );
									break;
								}
						}
						break;
					}
				default:
					{
						break;
					}
			}
		}

		void UpdatePonding ( )
		{
			int currentHeight = 0;
			if ( SourceObject . IsWet )
			{
				WaterLayer . Visibility = Visibility . Visible;
				currentHeight += SourceObject . PondingAmount;
				( WaterLayer . RenderTransform as CompositeTransform ) . TranslateY = -( currentHeight / 20 );
			}
			else
			{
				WaterLayer . Visibility = Visibility . Collapsed;
			}
			if ( SourceObject . IsFrozen )
			{
				IceLayer . Visibility = Visibility . Visible;
				currentHeight += SourceObject . IceThickness;
				( IceLayer . RenderTransform as CompositeTransform ) . TranslateY = -( currentHeight / 20 );
			}
			else
			{
				IceLayer . Visibility = Visibility . Collapsed;
			}
			if ( SourceObject . IsBearSnow )
			{
				SnowLayer . Visibility = Visibility . Visible;
				currentHeight += SourceObject . SnowThickness;
				( SnowLayer . RenderTransform as CompositeTransform ) . TranslateY = -( currentHeight / 20 );
			}
			else
			{
				SnowLayer . Visibility = Visibility . Collapsed;
			}
		}

		public void Update ( )
		{
			UpdateDirection ( );
			UpdatePonding ( );
		}

		public NormalRoad SourceObject
		{
			get { return ( NormalRoad ) GetValue ( SourceObjectProperty ); }
			set
			{
				if ( value != SourceObject )
				{
					SetValue ( SourceObjectProperty , value );
					Update ( );
				}
			}
		}

		public static readonly DependencyProperty SourceObjectProperty =
			DependencyProperty . Register ( nameof ( SourceObject ) , typeof ( NormalRoad ) , typeof ( NormalRoadBlock ) , new PropertyMetadata ( null ) );





	}
}
